// README
//
// There are two steps to adding a property:
//
// 1. Create a member variable to store your property
// 2. Add the get_ and set_ accessors for your property
//
// Remember that both are case sensitive!


/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />
/// <reference name="AjaxControlToolkit.ExtenderBase.BaseScripts.js" assembly="AjaxControlToolkit" />

Type.registerNamespace('InlineEdit');

InlineEdit.InlineEditBehavior = function(element) {
    InlineEdit.InlineEditBehavior.initializeBase(this, [element]);

    // Property variables:
    this._LabelControlIDValue = null;
    this._LabelHoverCssClassValue = null;
    this._LabelNormalCssClassValue = null;
    this._TargetCssClassValue = null;
    this._OnClientTargetValueChangedValue = null;
}

InlineEdit.InlineEditBehavior.prototype = {
    initialize : function() {
        InlineEdit.InlineEditBehavior.callBaseMethod(this, 'initialize');
        
        if (this.get_element().type == 'text' && this.get_element().value.trim() == '') {
            this.get_element().style.display = 'block';
        } else {
            this.get_element().style.display = 'none';
        }
                
        var controlType = this.get_element().type;            
        switch(controlType) {
            case "select-one":
                $get(this._LabelControlIDValue).innerHTML = this.get_element().options[this.get_element().selectedIndex].text;
                $addHandlers(this.get_element(), {
                             "change" : this.Dropdown_Change,
                             "blur" : this.Target_Blur,
                             "keydown" : this.Target_Keydown }, 
                             this);
                $addHandlers($get(this._LabelControlIDValue), {
                             "click" : this.Label_Click,
                             "mouseover" : this.Label_Mouseover,
                             "mouseout" : this.Label_Mouseout },
                             this);
                break;
            case "text":
                $get(this._LabelControlIDValue).innerHTML = this.get_element().value;
                $addHandlers(this.get_element(), {
                             "change" : this.Textbox_Change,
                             "blur" : this.Target_Blur,
                             "keydown" : this.Target_Keydown },
                             this);
                $addHandlers($get(this._LabelControlIDValue), {
                             "click" : this.Label_Click,
                             "mouseover" : this.Label_Mouseover,
                             "mouseout" : this.Label_Mouseout },
                             this);
                break;
        }
                     
        $get(this._LabelControlIDValue).className = this._LabelNormalCssClassValue;
        
        this.get_element().className = this._TargetCssClassValue;
    },

    dispose : function() {
        // Cleanup code goes here:
        $clearHandlers(this.get_element());
        $clearHandlers($get(this._LabelControlIDValue));
        InlineEdit.InlineEditBehavior.callBaseMethod(this, 'dispose');
    },
    
    Textbox_Change : function(e) {
        $get(this._LabelControlIDValue).innerHTML = this.get_element().value;
        if (this._OnClientTargetValueChangedValue) {
            window[this._OnClientTargetValueChangedValue]();
        }
    },
    
    Dropdown_Change : function(e) {
        $get(this._LabelControlIDValue).innerHTML = this.get_element().options[this.get_element().selectedIndex].text;
        this.get_element().style.display = 'none';
        $get(this._LabelControlIDValue).style.display = '';
        if (this._OnClientTargetValueChangedValue) {
            window[this._OnClientTargetValueChangedValue]();
        }
    },
    
    Target_Blur : function(e) {
        if (this.get_element().type == 'text' && this.get_element().value.trim() == '') {
            this.get_element().style.display = '';
            $get(this._LabelControlIDValue).style.display = 'none';
        } else {
            this.get_element().style.display = 'none';
            $get(this._LabelControlIDValue).style.display = '';
        }
    },
    
    Target_Keydown : function(event) {
        if (event.keyCode == 13) {
            event.preventDefault(); 
            this.get_element().blur();
        }
        if (event.keyCode == 38 || event.keyCode == 40) {
            event.preventDefault();
        }
    },
    
    Label_Click : function(e) {
        $get(this._LabelControlIDValue).style.display = 'none';
        this.get_element().style.display = '';
        this.get_element().focus();
    },
    
    Label_Mouseover : function(e) {
        $get(this._LabelControlIDValue).className = this._LabelHoverCssClassValue;
    },
    
    Label_Mouseout : function(e) {
        $get(this._LabelControlIDValue).className = this._LabelNormalCssClassValue;
    },
    

    // LabelControlID property:
    get_LabelControlID : function() {
        return this._LabelControlIDValue;
    },
    set_LabelControlID : function(value) {
        this._LabelControlIDValue = value;
    },
    
    
    // LabelHoverCssClass property:
    get_LabelHoverCssClass : function() {
        return this._LabelHoverCssClassValue;
    },
    set_LabelHoverCssClass : function(value) {
        this._LabelHoverCssClassValue = value;
    }, 
    
    
    // LabelNormalCssClass property:
    get_LabelNormalCssClass : function() {
        return this._LabelNormalCssClassValue;
    },
    set_LabelNormalCssClass : function(value) {
        this._LabelNormalCssClassValue = value;
    },
    
    
    // TargetCssClass property:
    get_TargetCssClass : function() {
        return this._TargetCssClassValue;
    },
    set_TargetCssClass : function(value) {
        this._TargetCssClassValue = value;
    },
    
    
    // OnClientTargetValueChanged property:
    get_OnClientTargetValueChanged : function() {
        return this._OnClientTargetValueChangedValue;
    },
    set_OnClientTargetValueChanged : function(value) {
        this._OnClientTargetValueChangedValue = value;
    }
    
}
InlineEdit.InlineEditBehavior.registerClass('InlineEdit.InlineEditBehavior', AjaxControlToolkit.BehaviorBase);
